﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.ObjectModel;

namespace babynames
{
    public class Parser
    {
        private String _filepath;
        List<BabyName> babyNamesCollection = new List<BabyName>();

        public String Filepath
        {
        set { _filepath = value; }
        get { return _filepath; }
        }

        public Parser(String filepath)
        {
            Filepath = filepath;
            ReadBabiesFromFile();
        }

        private void ReadBabiesFromFile()
        {
            try
            {
                var babyNamesArray = System.IO.File.ReadAllLines(Filepath);

                for (int i = 0; i < babyNamesArray.Count(); i++)
                {
                    var temp = new BabyName(babyNamesArray[i].ToString());
                    babyNamesCollection.Add(temp);
                }
            }

            catch (Exception)
            {
                throw new Exception("Fuck..");
            }
        }

        public ObservableCollection<String> TopTenBabyNames(int decade)
        {
            var resultList = new ObservableCollection<String>();

            int i = 0;
            foreach(BabyName babyname in babyNamesCollection)
            {
                if (babyname.Rank(decade) <= 10 && babyname.Rank(decade) > 0)
                    resultList.Add(babyname.Name);
                ++i;
            }

            return resultList;
        }

        public ObservableCollection<String> BabySearchQuery(String Name, out int AvgRank, out int Trend)
        {
            var resultList = new ObservableCollection<String>();

            foreach(BabyName babyName in babyNamesCollection)
            {
                if(babyName.Name == Name)
                {
                    AvgRank = babyName.AverageRank();
                    Trend = babyName.Trend();

                    for (int i = 1900; i <= 2000 ; i+=10)
                    {
                        resultList.Add(i + "    " + babyName.Rank(i));
                    }
                    return resultList;
                }
            }
            AvgRank = 0;
            Trend = 0;
            resultList.Add("Not found");
            return resultList;
        }
    }

}
